var net = require('net');
var fs = require('fs');

var server = net.createServer(function (socket)
{
    var buffer = new Buffer(0, 'binary');

    socket.on('data', function (data)
    {
        buffer = Buffer.concat([buffer, new Buffer(data, 'binary')]);
    });

    socket.on("end", function (data)
    {
        fs.writeFile("image.jpg", buffer);
        buffer = new Buffer(0, 'binary');
    });
});

server.listen(9090, '127.0.0.1');